package cn.fl.mcr.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cmm.mid.core.framework.constant.tripartite.InterfaceConstants;
import cmm.mid.core.framework.constant.tripartite.InterfaceConstants.AuditResult;
import cmm.mid.core.framework.dto.MessageDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.capital.condition.CapFundVfDtlQueryCondition;
import cn.fl.capital.dto.CapFundVfDtlDTO;
import cn.fl.capital.service.ICapFundVfDtlApiService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrBillBuyBackRecordService;
import cn.fl.mcr.service.IMcrBillMService;
import cn.fl.mcr.service.IMcrInformTransferService;
import cn.fl.mcr.transform.McrBillMVOTransform;
import cn.fl.mcr.transform.McrLoanAccountVOTransform;
import cn.fl.mcr.transform.McrLoanChannelVOTransform;
import cn.fl.mcr.util.ExcelReportUtil;
import cn.fl.mcr.vo.McrBillBuyBackRecordVO;
import cn.fl.mcr.vo.McrBillMVO;
import cn.fl.mcr.vo.McrBuyBackFailVO;
import cn.fl.mcr.vo.McrLoanAccountVO;
import cn.fl.mcr.vo.McrLoanChannelVO;
import cn.fl.system.constant.SysLogConstant;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.CapOverdueDtlCondition;
import cn.lionbridgecapital.loan.model.condition.McrBillBuyBackRecordQC;
import cn.lionbridgecapital.loan.model.condition.McrBillDQC;
import cn.lionbridgecapital.loan.model.condition.McrBillMQC;
import cn.lionbridgecapital.loan.model.condition.McrInformTransferQC;
import cn.lionbridgecapital.loan.model.condition.McrLoanAccountQC;
import cn.lionbridgecapital.loan.model.condition.McrLoanChannelQC;
import cn.lionbridgecapital.loan.model.constant.McrBillMConstant;
import cn.lionbridgecapital.loan.model.dto.CapOverdueInfoBean;
import cn.lionbridgecapital.loan.model.dto.McrBillBuyBackRecordDTO;
import cn.lionbridgecapital.loan.model.dto.McrBillDDTO;
import cn.lionbridgecapital.loan.model.dto.McrBillMDTO;
import cn.lionbridgecapital.loan.model.dto.McrInformTransferDTO;
import cn.lionbridgecapital.loan.model.dto.McrLoanAccountDTO;
import cn.lionbridgecapital.loan.model.utils.DateUtils;
import cn.lionbridgecapital.loan.model.utils.StringUtils;
import cn.lionbridgecapital.loan.service.api.IMcrBillDApiService;
import cn.lionbridgecapital.loan.service.api.IMcrBillMApiService;
import cn.lionbridgecapital.loan.service.api.IMcrLoanAccountApiService;
import cn.lionbridgecapital.loan.service.api.IMcrLoanChannelApiService;
import cn.lionbridgecapital.loan.utils.BaseLogUtils;
import fl.yntrust.facade.api.IYnTrustApi;
import fl.yntrust.facade.dto.CapOverdueDtlDTO;
import fl.yntrust.facade.dto.RepurchaseDTO;
import fl.yntrust.facade.dto.RepurchaseDetail;
import pam.wkxt.facade.api.IWkxtApiService;
import pam.wkxt.facade.dto.PrepaymentDTO;


/**
 * @Description: 账单回购记录 控制层
 */
@Controller
@RequestMapping("/mcr/mcrBillBuyBackRecord")
public class McrBillBuyBackRecordController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(McrBillBuyBackRecordController.class);

	/**
	 * McrBillBuyBackRecord 服务层BEAN
	 */
	@Autowired
	private IMcrBillBuyBackRecordService mcrBillBuyBackRecordService;
    @Autowired
    private IMcrLoanAccountApiService publicMcrLoanAccountApiService;
    @Autowired
    private IMcrBillMService mcrBillMService;
    @Autowired
    private IMcrLoanChannelApiService publicMcrLoanChannelApiService;
    @Autowired
    private IMcrInformTransferService mcrInformTransferService;
    @Autowired
    private ICapFundVfDtlApiService capFundVfDtlApiService;
    @Autowired
    private IWkxtApiService wkxtApiService;
    @Autowired
	private IYnTrustApi ynTrustApi;
    @Autowired
    private IMcrBillMApiService publicMcrBillMApiService ;
    @Autowired
    private IMcrBillDApiService publicMcrBillDApiService;
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("mcr/mcrBillM/mcrBuyBackRecord");
		McrLoanAccountQC accountQc = new McrLoanAccountQC();
        List<McrLoanAccountVO> accountList = McrLoanAccountVOTransform.toVOList(publicMcrLoanAccountApiService.searchMcrLoanAccountDTOList(accountQc));
        view.addObject("accountList", accountList);
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}


	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		McrBillBuyBackRecordQC qc = new McrBillBuyBackRecordQC();
		
		String crtTmStr = request.getParameter("crtTmStr");//开始日期
		if(StringUtils.isNotEmpty(crtTmStr)){
       	 	qc.setCrtTmStr(crtTmStr);
        }
		String crtTmEnd = request.getParameter("crtTmEnd");//结束日期
		if(StringUtils.isNotEmpty(crtTmEnd)) {
			qc.setCrtTmEnd(crtTmEnd);
		}
		page.setCondition(qc);
		Pager pager = this.mcrBillBuyBackRecordService.searchListPage(page);
		List<McrBillBuyBackRecordVO> list = (List<McrBillBuyBackRecordVO>) pager.getResultList();
		ListPageVO<McrBillBuyBackRecordVO> listPage = new ListPageVO<McrBillBuyBackRecordVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	
	/**
	 * 
	 * @Title: exportexcel   
	 * @Description: 下载  
	 * @param: @param request
	 * @param: @param response      
	 * @return: void      
	 * @throws
	 */
	@SuppressWarnings(value = "unchecked")
    @RequestMapping(value = "/exportexcel.do", method = {RequestMethod.POST, RequestMethod.GET})
    public void exportexcel(HttpServletRequest request, HttpServletResponse response) {
		String batchNo = request.getParameter("batchNo");//回购批次号
		McrBillMQC billQc = new McrBillMQC();
		if(StringUtils.isNotEmpty(batchNo)){
            String[] split = batchNo.split(",");
            List<String> strings = Arrays.asList(split);
            billQc.setSqBuyBackBatchNoList(strings);
        }else {
        	BaseLogUtils.info(logger, "exportexcel", "exportexcel 导出失败,未选中数据" , CurrentThreadContext.getCurrentUserName());
        	return ;
        }
		List<McrBillMVO> billList = mcrBillMService.searchList(billQc);
		List<Map<String, Object>> contentMap = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(billList)) {
			for(McrBillMVO vo:billList) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("crCntNo", vo.getCrCntNo());//合同号
				map.put("cstNm", vo.getCstNm());//合同号
				map.put("crAmt", DecimalUtils.convertFenToYuan(vo.getCrAmt()));//借款金额
				String repSts = vo.getRepSts();
				if("00".equals( repSts)) {
					repSts = "可回购";
				}else if("01".equals( repSts)) {
					repSts = "待回购";
				}else if("02".equals( repSts)) {
					repSts = "回购成功";
				}else{
					repSts = "回购失败";
				}
				map.put("repSts", repSts);//回购状态
				McrInformTransferQC transferQc = new McrInformTransferQC();
				transferQc.setCrCntNo(vo.getCrCntNo());
				List<McrInformTransferDTO> searchMcrInformTransferDTOList = mcrInformTransferService.searchMcrInformTransferDTOList(transferQc);
				if(CollectionUtils.isNotEmpty(searchMcrInformTransferDTOList)) {
					McrInformTransferDTO mcrInformTransferDTO = searchMcrInformTransferDTOList.get(0);
					McrLoanAccountDTO accountDto = publicMcrLoanAccountApiService.selectMcrLoanAccountDTOById(mcrInformTransferDTO.getLoanAccountId());
					if(null != accountDto) {
						map.put("loanName", McrBillMConstant.LOAN_CHANNEL_DESC_MAP.get(accountDto.getLoanChannel()));//资方名称
						map.put("accountName", accountDto.getAccountName());//项目名称
					}
					map.put("capAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getCapAmt()));//借款狮桥回购本金
					map.put("intrAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getIntrAmt()));//狮桥回购利息
					map.put("penaltyAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getPenaltyAmt()));//狮桥回购罚息
					map.put("totalAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getTotalAmt()));//狮桥回购金额
					map.put("yhCapAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getYhCapAmt()));//资方回购本金
					map.put("yhIntrAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getYhIntrAmt()));//资方回购利息
					map.put("yhPenaltyAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getYhPenaltyAmt()));//资方回购罚息
					map.put("yhTotalAmt", DecimalUtils.convertFenToYuan(mcrInformTransferDTO.getYhTotalAmt()));//资方回购金额
					map.put("crtTm", DateUtils.formatDateToString(mcrInformTransferDTO.getCrtTm(), DateUtils.YYYY_MM_DDHHMMSS) );//回购时间
				}
				contentMap.add(map);
			}
			
		}
		
		String[][] headers = {{"合同号","用户姓名","借款金额","借款狮桥回购本金","狮桥回购利息","狮桥回购罚息","狮桥回购金额","回购状态","资方名称","项目名称","资方回购本金","资方回购利息","资方回购罚息","资方回购金额","回购时间"}
		,{"crCntNo","cstNm","crAmt","capAmt","intrAmt","penaltyAmt","totalAmt", "repSts", "loanName","accountName","yhCapAmt","yhIntrAmt","yhPenaltyAmt","yhTotalAmt","crtTm"}};
		BaseLogUtils.info(logger, "exportexcel", "回购明细生成报表导出excel开始",CurrentThreadContext.getCurrentUserName());
		ExcelReportUtil.exportExcelReportPage("hg", headers, contentMap, response, request);
		BaseLogUtils.info(logger, "exportexcel", "回购明细成报表导出excel完成",CurrentThreadContext.getCurrentUserName());
	}
	
	
	/**
	 * 
	 * @Title: mcrBuyBackFail   
	 * @Description: 跳转回购异常界面
	 * @param: @param request
	 * @param: @param response
	 * @param: @return
	 * @param: @throws BaseException      
	 * @return: ModelAndView      
	 * @throws
	 */
	@RequestMapping(value = "/mcrBuyBackFailPage.do", method = {RequestMethod.GET})
    public ModelAndView mcrBuyBackFailPage(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        BaseLogUtils.info(logger, "toListPage", "加载mcrBuyBackDetail详情页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("mcr/mcrBillM/mcrBuyBackFail");
        String batchNo = request.getParameter("batchNo");
        BaseLogUtils.info(logger, "mcrBuyBackFailPage", "mcrBuyBackFailPage>>batchNo:"+batchNo,CurrentThreadContext.getCurrentUserName());
        mav.addObject("batchNo", batchNo);
      //资方渠道
        McrLoanChannelQC channelQc = new McrLoanChannelQC();
        List<McrLoanChannelVO> channelList = McrLoanChannelVOTransform.toVOList(publicMcrLoanChannelApiService.searchMcrLoanChannelDTOList(channelQc));
        mav.addObject("channelList", channelList);
        return mav;
    }
	
	/**
	 * 
	 * @Title: mcrBuyBackFail   
	 * @Description: 购异常界面组装数据  
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: Object      
	 * @throws
	 */
	@RequestMapping(value = "/mcrBuyBackFail.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object mcrBuyBackFail(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		String batchNo = request.getParameter("batchNo");
		BaseLogUtils.info(logger, "mcrBuyBackFail", "mcrBuyBackFail>>batchNo:"+batchNo,CurrentThreadContext.getCurrentUserName());
		List<McrBuyBackFailVO> selectBuyBackList = mcrInformTransferService.selectBuyBackList(batchNo,0l);
        ListPageVO<McrBuyBackFailVO> listPage = null;
        if(CollectionUtils.isNotEmpty(selectBuyBackList)) {
        	listPage = new ListPageVO<McrBuyBackFailVO>(selectBuyBackList, selectBuyBackList.size());
        }
        BaseLogUtils.info(logger, "mcrBuyBackFail", "mcrBuyBackFail>>response:"+JSON.toJSONString(listPage),CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 
	 * @Title: mcrBuyBackFailDetailPage   
	 * @Description: 人工修改回购信息页面  
	 * @param: @param request
	 * @param: @param response
	 * @param: @return
	 * @param: @throws BaseException      
	 * @return: ModelAndView      
	 * @throws
	 */
	@RequestMapping(value = "/mcrBuyBackFailDetailPage.do", method = {RequestMethod.GET})
    public ModelAndView mcrBuyBackFailDetailPage(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        BaseLogUtils.info(logger, "toListPage", "加载mcrBuyBackFailDetail详情页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("mcr/mcrBillM/mcrBuyBackFailDetail");
        String billId = request.getParameter("billId");
        mav.addObject("billId", billId);
        return mav;
    }
	
	/**
	 * 
	 * @Title: mcrBuyBackFailDetail   
	 * @Description: 组装人工修改回购信息
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: Object      
	 * @throws
	 */
	@RequestMapping(value = "/mcrBuyBackFailDetail.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object mcrBuyBackFailDetail(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		
		String billId = request.getParameter("billId");
		Long id = null;
		if(StringUtils.isNotBlank(billId)) {
			id = Long.valueOf(billId);
		}
		List<McrBuyBackFailVO> selectBuyBackList = mcrInformTransferService.selectBuyBackList("",id);
		ListPageVO<McrBuyBackFailVO> listPage = null;
        if(CollectionUtils.isNotEmpty(selectBuyBackList)) {
        	listPage = new ListPageVO<McrBuyBackFailVO>(selectBuyBackList, selectBuyBackList.size());
        }
        BaseLogUtils.info(logger, "mcrBuyBackFailDetail", "mcrBuyBackFailDetail>>response:"+JSON.toJSONString(listPage),CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	
	@RequestMapping(value = "/mcrBuyBackFailcommit.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO mcrBuyBackFailcommit(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		String billId = request.getParameter("billId");//订单id
		String sqBuyBackBatchNo = request.getParameter("sqBuyBackBatchNo");//狮桥回购批次号
		if(StringUtils.isBlank(sqBuyBackBatchNo)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("回购批次号为空!");
			return result;
		}
		McrBillBuyBackRecordQC recQc = new McrBillBuyBackRecordQC();
		recQc.setBatchNo(sqBuyBackBatchNo);
		List<McrBillBuyBackRecordDTO> buyBackRecordList = mcrBillBuyBackRecordService.searchList(recQc);
		if(CollectionUtils.isEmpty(buyBackRecordList)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("没有回购记录");
			return result;
		}
		
		String totalAmt = request.getParameter("totalAmt");//狮桥回购总金额
		String capAmt = request.getParameter("capAmt");//狮桥未还本金
		String intrAmt = request.getParameter("intrAmt");//狮桥未还利息
		String penaltyAmt = request.getParameter("penaltyAmt");//狮桥未还罚息
		String yhTotalAmt = request.getParameter("yhTotalAmt");//银行应回购总金额
		String yhCapAmt = request.getParameter("yhCapAmt");//银行应回购本金
		String yhIntrAmt = request.getParameter("yhIntrAmt");//银行应回购利息
		String yhPenaltyAmt = request.getParameter("yhPenaltyAmt");//银行应回购罚息
		
		McrBillMQC qc = new McrBillMQC();
		qc.setId(Long.valueOf(billId));
		List<McrBillMVO> searchList = mcrBillMService.searchList(qc);
		if(CollectionUtils.isNotEmpty(searchList)) {
			
			//当天有还款不能回购
			CapFundVfDtlQueryCondition capFundVfDtlQueryCondition = new CapFundVfDtlQueryCondition();
			capFundVfDtlQueryCondition.setPaySchId(Long.valueOf(billId));
			capFundVfDtlQueryCondition.setVfStsCd("2");
			capFundVfDtlQueryCondition.setVfDt(new Date());
			BaseLogUtils.info(logger, "mcrBuyBackFailcommit", "判断所选数据是否可以回购.CapFundVfDtlQueryCondition:" + JSONObject.toJSONString(capFundVfDtlQueryCondition), CurrentThreadContext.getCurrentUserName());
			List<CapFundVfDtlDTO> list = capFundVfDtlApiService.searchCapFundVerificationList(capFundVfDtlQueryCondition);
			BaseLogUtils.info(logger, "mcrBuyBackFailcommit", "判断所选数据是否可以回购.searchCapFundVerificationList:" + JSONObject.toJSONString(list), CurrentThreadContext.getCurrentUserName());
			if(CollectionUtils.isNotEmpty(list)) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("当天有还款,不允许回购");
				return result;
			}
			
			Long loanAccountId = 0l;
			McrInformTransferQC informTransferQc = new McrInformTransferQC();
			List<McrInformTransferDTO> searchMcrInformTransferDTOList = mcrInformTransferService.searchMcrInformTransferDTOList(informTransferQc);
			if(CollectionUtils.isNotEmpty(searchMcrInformTransferDTOList)) {
				loanAccountId = searchMcrInformTransferDTOList.get(0).getLoanAccountId();
			}
			
			McrLoanAccountQC accountQc = new McrLoanAccountQC();
			accountQc.setId(loanAccountId);
			List<McrLoanAccountDTO> accountList = publicMcrLoanAccountApiService.searchList(accountQc);
			String loanChannel = "";
			if(CollectionUtils.isNotEmpty(accountList)) {
				loanChannel = accountList.get(0).getLoanChannel();
			}
			
			BaseLogUtils.info(logger, "mcrBuyBackFailcommit", "loanChannel:" + loanChannel, CurrentThreadContext.getCurrentUserName());
			McrBillMDTO dto = McrBillMVOTransform.toDTO(searchList.get(0)) ;
			if(McrBillMConstant.LOAN_CHANNEL_WKXT.equals(loanChannel)){
				result = this.sendWKBuyBack(result, dto, Long.valueOf(capAmt), Long.valueOf(intrAmt), Long.valueOf(penaltyAmt), Long.valueOf(totalAmt), Long.valueOf(yhCapAmt), Long.valueOf(yhIntrAmt),Long.valueOf(yhPenaltyAmt), Long.valueOf(yhTotalAmt));
			}else if(McrBillMConstant.LOAN_CHANNEL_YNXT.equals(loanChannel)) {
				result = this.sendYXBuyBack(result, dto, Long.valueOf(capAmt), Long.valueOf(intrAmt), Long.valueOf(penaltyAmt), Long.valueOf(totalAmt), Long.valueOf(yhCapAmt), Long.valueOf(yhIntrAmt),Long.valueOf(yhPenaltyAmt), Long.valueOf(yhTotalAmt));
			}else if(McrBillMConstant.LOAN_CHANNEL_SNYH.equals(loanChannel)) {
				
			}else if(McrBillMConstant.LOAN_CHANNEL_YLYH.equals(loanChannel)) {
				result = this.sendYLBuyBack(result, dto, Long.valueOf(capAmt), Long.valueOf(intrAmt), Long.valueOf(penaltyAmt), Long.valueOf(totalAmt), Long.valueOf(yhCapAmt), Long.valueOf(yhIntrAmt),Long.valueOf(yhPenaltyAmt), Long.valueOf(yhTotalAmt));
			}
			
			McrBillBuyBackRecordDTO mcrBillBuyBackRecordDTO = buyBackRecordList.get(0);
			if(BaseConstant.IS_YESNO_YES == result.getSuccess()) {
				mcrBillBuyBackRecordDTO.setSuccessNumber(mcrBillBuyBackRecordDTO.getSuccessNumber() +1);
				mcrBillBuyBackRecordDTO.setFailNumber(mcrBillBuyBackRecordDTO.getFailNumber() -1);
				mcrBillBuyBackRecordDTO.setMdfTm(new Date());
				mcrBillBuyBackRecordService.modify(mcrBillBuyBackRecordDTO);
			}
			
		}
		return result;
	}
	
	/**
	 * 
	 * @Title: sendWKBuyBack   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param result
	 * @param: @param dto
	 * @param: @param capAmt
	 * @param: @param intrAmt
	 * @param: @param penaltyAmt
	 * @param: @param totalAmt
	 * @param: @param yhCapAmt
	 * @param: @param yhIntrAmt
	 * @param: @param yhPenaltyAmt
	 * @param: @param yhTotalAmt
	 * @param: @return      
	 * @return: DataResultVO      
	 * @throws
	 */
    public DataResultVO sendWKBuyBack(DataResultVO result,McrBillMDTO dto,Long capAmt,Long intrAmt,Long penaltyAmt,Long totalAmt,Long yhCapAmt,Long yhIntrAmt,Long yhPenaltyAmt,Long yhTotalAmt) {
    	try {
    		BaseLogUtils.info(logger, "sendWKBuyBack", "McrBillBuyBackRecordController.sendWKBuyBack 五矿推送回购数据.账单明细：" + JSONObject.toJSONString(dto), CurrentThreadContext.getCurrentUserName());
    		
    		List<PrepaymentDTO> prepaymentDTOS = Lists.newArrayList();
    		PrepaymentDTO prepaymentDto = new PrepaymentDTO();
    		McrLoanAccountQC condition = new McrLoanAccountQC();
    		condition.setIsDel(BaseConstant.IS_YESNO_NO);
    		condition.setIsValid(BaseConstant.IS_YESNO_YES);
    		condition.setLoanChannel("WKXT");
    		List<McrLoanAccountDTO> dtoList = publicMcrLoanAccountApiService.searchList(condition);
    		if(CollectionUtils.isNotEmpty(dtoList)) {
    			prepaymentDto.setFundRasInsId(dtoList.get(0).getLoanId());//机构ID
    			prepaymentDto.setCstId(dto.getCstId());//客户ID
    			prepaymentDto.setBillId(dto.getId());//账单ID
    			prepaymentDto.setLsCntNo(dto.getCrCntNo());//我司合同号
    			prepaymentDto.setProjNo(dtoList.get(0).getProjectCode());//项目号
    			prepaymentDto.setRepPrcp(capAmt);//债转本金
    			prepaymentDto.setRepInte(intrAmt);//债转利息
    			prepaymentDto.setRepFine(penaltyAmt);//债转罚息
    			prepaymentDTOS.add(prepaymentDto);
    			BaseLogUtils.info(logger, "sendWKBuyBack", "McrBillBuyBackRecordController.sendWKBuyBack 五矿推送回购数据>>>>>>：" + JSONObject.toJSONString(prepaymentDTOS), CurrentThreadContext.getCurrentUserName());
    			MessageDTO<AuditResult> prepaymentMessage = wkxtApiService.prepayment(prepaymentDTOS);
    			BaseLogUtils.info(logger, "sendWKBuyBack", "McrBillBuyBackRecordController.sendWKBuyBack 五矿推送回购数据返回结果>>>>>>：" + JSONObject.toJSONString(prepaymentMessage), CurrentThreadContext.getCurrentUserName());
    			if(prepaymentMessage!=null && prepaymentMessage.isSuccess() && null != prepaymentMessage.getData() && prepaymentMessage.getData().equals(InterfaceConstants.AuditResult.pass)){
    				BaseLogUtils.info(logger, "sendWKBuyBack", "五矿推送回购数据.推送明细成功!.账单明细：" + JSONObject.toJSONString(dto.getId()), CurrentThreadContext.getCurrentUserName());
    				this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
    				publicMcrBillMApiService.wkPrepaymentResult(dto.getId(), prepaymentMessage.getCode(),dto.getSqBuyBackBatchNo());
    			}else {
    				String message = prepaymentMessage.getMessage();//失败原因
    				if(message.length() > 0) {
    					McrBillMDTO billDto = new  McrBillMDTO();
    					billDto.setId(dto.getId());
    					if(message.length() > 1000) {
    						message = message.substring(0,999);
    					}
    					billDto.setBuyBackFailReason(message);//失败原因
    					billDto.setSqBuyBackBatchNo(dto.getSqBuyBackBatchNo());
    					billDto.setRepSts("03");
    					
    					this.updateMcrInformTransfer(billDto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
    					this.publicMcrBillMApiService.modifyMcrBillM(billDto);
    				}
    				BaseLogUtils.info(logger, "sendWKBuyBack", "McrBillBuyBackRecordController.sendWKBuyBack 五矿推送回购数据.推送明细失败!.账单明细：" + JSONObject.toJSONString(dto.getId()), CurrentThreadContext.getCurrentUserName());
    				result.setSuccess(BaseConstant.IS_YESNO_NO);
    				result.setInfo("推送回购数据.推送罚息明细失败!.账单明细："+dto.getId());
    				return result;
    			}
    		}else {
    			BaseLogUtils.info(logger, "sendWKBuyBack", "五矿基本信息为空,无法获取机构,项目号等信息！");
    			result.setSuccess(BaseConstant.IS_YESNO_NO);
    			result.setInfo("五矿基本信息为空,无法获取机构,项目号等信息！");
    			return result;
    		}
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误!");
            BaseLogUtils.error(logger, "sendWKBuyBack", "推送回购数据失败:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
    	
    	return result;
    }
    
    /**
     * 
     * @Title: sendYXBuyBack   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param result
     * @param: @param dto
     * @param: @param capAmt
     * @param: @param intrAmt
     * @param: @param penaltyAmt
     * @param: @param totalAmt
     * @param: @param yhCapAmt
     * @param: @param yhIntrAmt
     * @param: @param yhPenaltyAmt
     * @param: @param yhTotalAmt
     * @param: @return      
     * @return: DataResultVO      
     * @throws
     */
    public DataResultVO sendYXBuyBack(DataResultVO result,McrBillMDTO dto,Long capAmt,Long intrAmt,Long penaltyAmt,Long totalAmt,Long yhCapAmt,Long yhIntrAmt,Long yhPenaltyAmt,Long yhTotalAmt) {
    	try {
    		BaseLogUtils.info(logger, "sendYXBuyBack", "云信推送回购数据.账单明细：" + JSONObject.toJSONString(dto), CurrentThreadContext.getCurrentUserName());
    		//推送云信逾期数据
        	DataResultVO sendYXBuyBackOverdue = this.sendYXBuyBackOverdue(result, dto);
        	if(0 == sendYXBuyBackOverdue.getSuccess()) {
        		return sendYXBuyBackOverdue;
        	}
        	result = this.sendYXBuyBackInfo(result, dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return result;
    }
    
    /**
     * 
     * @Title: sendYXBuyBackOverdue   
     * @Description: 推送云信回购逾期数据
     * @param: @param result
     * @param: @param dto
     * @param: @return      
     * @return: DataResultVO      
     * @throws
     */
    public DataResultVO sendYXBuyBackOverdue(DataResultVO result,McrBillMDTO dto) {
    	BaseLogUtils.info(logger, "sendYXBuyBackOverdue", "云信推送回购逾期数据.账单明细start：" + JSONObject.toJSONString(dto), CurrentThreadContext.getCurrentUserName());
    	try {
    		//单笔逾期罚息
            List<CapOverdueDtlDTO> capOverdueDtlList = new ArrayList<CapOverdueDtlDTO>();
            CapOverdueDtlCondition capOverdueDtlQC = new CapOverdueDtlCondition();
            capOverdueDtlQC.setPaySchId(dto.getId());
            capOverdueDtlQC.setIsDel(BaseConstant.IS_YESNO_NO);
            capOverdueDtlQC.setPenaltyDt(DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD));
            List<CapOverdueInfoBean> dtlList = publicMcrBillMApiService.queryCapOverdueInfoDTO(capOverdueDtlQC);
            BaseLogUtils.info(logger, "sendYXBuyBackOverdue", "推送云信回购数据.推送罚息明细.罚息数据明细:" + JSONObject.toJSONString(dtlList), CurrentThreadContext.getCurrentUserName());
            McrBillMQC mcrBillMQC = new McrBillMQC();
            mcrBillMQC.setId(dto.getId());
            McrBillMDTO billM = publicMcrBillMApiService.searchMcrBillMList(mcrBillMQC).get(0);
            if (CollectionUtils.isNotEmpty(dtlList)) {
                for (CapOverdueInfoBean dtl : dtlList) {
                    McrBillDQC billDQC = new McrBillDQC();
                    billDQC.setBillId(dto.getId());
                    billDQC.setPayPd(dtl.getPayPd());
                    McrBillDDTO billD = publicMcrBillDApiService.searchMcrBillDList(billDQC).get(0);
                    CapOverdueDtlDTO overdueDtlDTO = new CapOverdueDtlDTO();
                    overdueDtlDTO.setId(billD.getId());
                    overdueDtlDTO.setBillId(dtl.getPaySchId());
                    overdueDtlDTO.setPayPd(dtl.getPayPd().intValue());
                    overdueDtlDTO.setPaidUpAmt(new BigDecimal(dtl.getPenaltyAmount()+(dtl.getPayAmount() == null ? 0 : dtl.getPayAmount()) + (dtl.getBeforePayAmount() == null ? 0 : dtl.getBeforePayAmount())));
                    BaseLogUtils.info(logger, "sendYXBuyBackOverdue", "推送云信回购数据.推送罚息明细.罚息数据明细.罚息(paidUpAmt):" + overdueDtlDTO.getPaidUpAmt(), CurrentThreadContext.getCurrentUserName());
                    overdueDtlDTO.setFundRasInsId(billM.getLoanId().longValue());
                    overdueDtlDTO.setFundRasInsNm(billM.getLoanName());
                    overdueDtlDTO.setLsCntNo(billM.getCrCntNo());
                    overdueDtlDTO.setCstId(billM.getCstId());
                    overdueDtlDTO.setCstNm(billM.getCstNm());
                    String loanNo =  publicMcrBillMApiService.queryInfTransLog(billM.getLoanId().toString(), billM.getId().toString(),"createPayPlan","0000");
                    BaseLogUtils.info(logger, "sendYXBuyBackOverdue", "推送云信回购数据.推送罚息明细.合同唯一编号:" + JSONObject.toJSONString(loanNo), CurrentThreadContext.getCurrentUserName());
                    if(StringUtils.isEmpty(loanNo)){
                        result.setSuccess(BaseConstant.IS_YESNO_NO);
                        result.setInfo("推送罚息明细.合同唯一编号为空 -客户名称:"+dto.getCstNm());
                        return result;
                    }
                    overdueDtlDTO.setLoanNo(loanNo);
                    capOverdueDtlList.add(overdueDtlDTO);
                }
            }
            if(CollectionUtils.isNotEmpty(capOverdueDtlList)){
                MessageDTO<String> message = ynTrustApi.repurchaseUpdateOverdue(capOverdueDtlList);
                BaseLogUtils.info(logger, "sendYXBuyBackOverdue", "推送云信回购数据.推送罚息明细.返回数据:" + JSONObject.toJSONString(message), CurrentThreadContext.getCurrentUserName());
                if(message == null || message.isSuccess() == false){
                	String returnMsg = message.getMessage();
                	if(returnMsg.length() > 0) {
                		McrBillMDTO billDto = new  McrBillMDTO();
                		billDto.setId(dto.getId());
                		if(returnMsg.length() > 1000) {
                			returnMsg = returnMsg.substring(0,999);
    					}
    					billDto.setBuyBackFailReason(returnMsg);//失败原因
    					billDto.setRepSts("03");
                		this.publicMcrBillMApiService.modifyMcrBillM(billDto);
                	}
                	
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("推送回购数据.推送罚息明细失败!.账单明细："+dto.getId());
                    BaseLogUtils.info(logger, "sendYXBuyBackOverdue", "推送云信回购数据.推送罚息明细失败!.账单明细：" + JSONObject.toJSONString(dto.getId()), CurrentThreadContext.getCurrentUserName());
                    return result;
                }
            }
    		
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误!");
            BaseLogUtils.error(logger, "sendYXBuyBackOverdue", "推送云信回购-逾期数据失败:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
    	
    	return result;
    }
    
    /**
     * 
     * @Title: sendYXBuyBackInfo   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param result
     * @param: @param dto
     * @param: @param getRepurchaseDetails
     * @param: @param capAmt
     * @param: @param intrAmt
     * @param: @param penaltyAmt
     * @param: @param totalAmt
     * @param: @param yhCapAmt
     * @param: @param yhIntrAmt
     * @param: @param yhPenaltyAmt
     * @param: @param yhTotalAmt
     * @param: @return      
     * @return: DataResultVO      
     * @throws
     */
    public DataResultVO sendYXBuyBackInfo(DataResultVO result,McrBillMDTO dto,Long capAmt,Long intrAmt,Long penaltyAmt,Long totalAmt,Long yhCapAmt,Long yhIntrAmt,Long yhPenaltyAmt,Long yhTotalAmt) {
    	BaseLogUtils.info(logger, "sendYXBuyBackInfo", "云信推送回购数据.账单明细start",  CurrentThreadContext.getCurrentUserName());
    	try {
    		Long insId = dto.getLoanAccountId();
        	String insNm = dto.getLoanName();
        	long accountId = dto.getLoanAccountId();
    		
    		RepurchaseDTO sendDto = new RepurchaseDTO();
    		McrLoanAccountDTO accountDto = publicMcrLoanAccountApiService.selectMcrLoanAccountDTOById(accountId);
            String productCode = accountDto.getProductCode();
            sendDto.setFundRasInsId(insId);//机构id
            sendDto.setFundRasInsNm(insNm);//机构名称
            sendDto.setProductCode(productCode);//产品编码
            
            List<RepurchaseDetail> getRepurchaseDetails = Lists.newArrayList();
          //回购
        	RepurchaseDetail detail = new RepurchaseDetail();
        	detail.setCstId(dto.getCstId());
        	detail.setCstNm(dto.getCstNm());
        	detail.setBillId(dto.getId());
        	detail.setLsCntNo(dto.getCrCntNo());
        	String infLoanNo = publicMcrBillMApiService.queryInfTransLog(dto.getLoanId().toString(), dto.getId().toString(), "queryLoan","1");
        	BaseLogUtils.info(logger, "hgSend", "推送回购数据.合同唯一编号:" + JSONObject.toJSONString(infLoanNo), CurrentThreadContext.getCurrentUserName());
        	if(StringUtils.isEmpty(infLoanNo)){
        		result.setSuccess(BaseConstant.IS_YESNO_NO);
        		result.setInfo("推送回购数据.合同唯一编号为空 -客户名称:"+dto.getCstNm());
        		return result;
        	}
        	detail.setUniqueid(infLoanNo);//合同唯一编号
        	detail.setPrincipal(new BigDecimal(DecimalUtils.convertFenToYuan(capAmt)).setScale(2,BigDecimal.ROUND_HALF_UP));
        	detail.setInterest(new BigDecimal(DecimalUtils.convertFenToYuan(intrAmt)).setScale(2,BigDecimal.ROUND_HALF_UP));
        	detail.setPenaltyInterest(new BigDecimal(DecimalUtils.convertFenToYuan(penaltyAmt)).setScale(2,BigDecimal.ROUND_HALF_UP));
        	detail.setAmount(new BigDecimal(DecimalUtils.convertFenToYuan(totalAmt)).setScale(2,BigDecimal.ROUND_HALF_UP));
        	getRepurchaseDetails.add(detail);
            
            
            sendDto.setRepurchaseDetails(getRepurchaseDetails);
            MessageDTO<String> message = ynTrustApi.repurchase(sendDto);
            BaseLogUtils.info(logger, "sendYXBuyBackInfo", "推送云信回购数据.返回数据:" + JSONObject.toJSONString(message), CurrentThreadContext.getCurrentUserName());
            if(message!=null && message.isSuccess() == true && message.getCode().equals("0000")){
                result.setInfo("操作成功.成功账单Id："+JSONObject.toJSONString(message.getDatas()));
                if(message.getDatas()!=null && message.getDatas().size()>0){
                	this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
                    
                	dto.setRepSts("02");
                    reSetMcrBillMDTO(dto);
                }else {
                	this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
                	dto.setRepSts("03");
                	this.publicMcrBillMApiService.modifyMcrBillM(dto);
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("回购申请接口操作失败");
                }
            }else{
            	this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
            	dto.setRepSts("03");
            	this.publicMcrBillMApiService.modifyMcrBillM(dto);
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("回购申请接口操作失败");
            }
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误!");
            BaseLogUtils.error(logger, "sendYXBuyBackInfo", "推送云信回购数据失败:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
    	
    	return result;
    }
    
    /**
     * 
     * @Title: updateMcrInformTransfer   
     * @Description: 修改明细  
     * @param: @param dto
     * @param: @param capAmt
     * @param: @param intrAmt
     * @param: @param penaltyAmt
     * @param: @param totalAmt
     * @param: @param yhCapAmt
     * @param: @param yhIntrAmt
     * @param: @param yhPenaltyAmt
     * @param: @param yhTotalAmt      
     * @return: void      
     * @throws
     */
    public void updateMcrInformTransfer(McrBillMDTO dto,Long capAmt,Long intrAmt,Long penaltyAmt,Long totalAmt,Long yhCapAmt,Long yhIntrAmt,Long yhPenaltyAmt,Long yhTotalAmt) {
    	BaseLogUtils.info(logger, "addMcrInformTransfer", "McrBillMController.addMcrInformTransfer.start:" + JSONObject.toJSONString(dto), CurrentThreadContext.getCurrentUserName());
    	
    	McrInformTransferQC qc = new McrInformTransferQC();
    	qc.setPrjId(dto.getId());
    	List<McrInformTransferDTO> searchMcrInformTransferDTOList = mcrInformTransferService.searchMcrInformTransferDTOList(qc);
    	BaseLogUtils.info(logger, "addMcrInformTransfer", "McrBillMController.addMcrInformTransfer>>searchMcrInformTransferDTOList:" + JSONObject.toJSONString(searchMcrInformTransferDTOList), CurrentThreadContext.getCurrentUserName());
    	if(CollectionUtils.isNotEmpty(searchMcrInformTransferDTOList)) {
    		McrInformTransferDTO mcrInformTransferDTO = searchMcrInformTransferDTOList.get(0);
    		mcrInformTransferDTO.setCapAmt(capAmt);
    		mcrInformTransferDTO.setIntrAmt(intrAmt);
    		mcrInformTransferDTO.setPenaltyAmt(penaltyAmt);
    		mcrInformTransferDTO.setTotalAmt(totalAmt);
    		mcrInformTransferDTO.setYhCapAmt(yhCapAmt);
    		mcrInformTransferDTO.setYhIntrAmt(yhIntrAmt);
    		mcrInformTransferDTO.setYhPenaltyAmt(yhPenaltyAmt);
    		mcrInformTransferDTO.setYhTotalAmt(yhTotalAmt);
    		mcrInformTransferDTO.setMdfTm(new Date());
    		mcrInformTransferDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
    		mcrInformTransferService.modifyMcrInformTransferDTO(mcrInformTransferDTO);
    	}
        BaseLogUtils.info(logger, "addMcrInformTransfer", "McrBillMController.addMcrInformTransfer.end", CurrentThreadContext.getCurrentUserName());
    }
    
    /**
     * 
     * @Title: reSetMcrBillMDTO   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param mcrBillMDTO
     * @param: @return
     * @param: @throws BaseException      
     * @return: McrBillMDTO      
     * @throws
     */
    private McrBillMDTO reSetMcrBillMDTO(McrBillMDTO mcrBillMDTO) throws BaseException{
        mcrBillMDTO.setLoanSource(McrBillMConstant.LOAN_SOURCE_0);
        mcrBillMDTO.setLoanId(null);
        mcrBillMDTO.setLoanName(null);
        mcrBillMDTO.setLoanChannel(null);
        mcrBillMDTO.setLoanAccountId(null);
        mcrBillMDTO.setLoanAccountNm(null);
        mcrBillMDTO.setIsInform(1);
        int count = this.publicMcrBillMApiService.reSetMcrBillMDTO(mcrBillMDTO);
        BaseLogUtils.info(logger, "reSetMcrBillMDTO", "三方机构转我司.重置属性结果：" + count + "-账单：" + mcrBillMDTO.getId());
        return mcrBillMDTO;
    }
    
    
    /**
     * 
     * @Title: pullWKStatus   
     * @Description: 拉取五矿回购状态
     * @param: @param request
     * @param: @param response      
     * @return: void      
     * @throws
     */
    @RequestMapping(value = "/pullWKStatus.do", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object pullWKStatus(HttpServletRequest request, HttpServletResponse response) {
    	BaseLogUtils.info(logger, "pullWKStatus", "拉取五矿回购状态start" );
    	DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
    	try {
    		publicMcrBillMApiService.wkPrepaymentResult(0L, null, null);
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
	        result.setInfo("拉取五矿回购状态异常");
			BaseLogUtils.info(logger, "pullWKStatus", "拉取五矿回购状态异常:"+e.getMessage(), CurrentThreadContext.getCurrentUserName() );
		}
    	BaseLogUtils.info(logger, "pullWKStatus", "拉取五矿回购状态end" );
    	return result;
	}
    
    
    
    /**
     * 
     * @Title: sendYLBuyBack   
     * @Description: 推送亿联 
     * @param: @param result
     * @param: @param dto
     * @param: @param capAmt
     * @param: @param intrAmt
     * @param: @param penaltyAmt
     * @param: @param totalAmt
     * @param: @param yhCapAmt
     * @param: @param yhIntrAmt
     * @param: @param yhPenaltyAmt
     * @param: @param yhTotalAmt
     * @param: @return      
     * @return: DataResultVO      
     * @throws
     */
    public DataResultVO sendYLBuyBack(DataResultVO result,McrBillMDTO dto,Long capAmt,Long intrAmt,Long penaltyAmt,Long totalAmt,Long yhCapAmt,Long yhIntrAmt,Long yhPenaltyAmt,Long yhTotalAmt) {
    	try {
    		BaseLogUtils.info(logger, "sendYLBuyBack", "McrBillBuyBackRecordController.sendYLBuyBack 亿联推送回购数据.账单明细：" + JSONObject.toJSONString(dto), CurrentThreadContext.getCurrentUserName());
    		
    		//罚息
			if(penaltyAmt.longValue() !=yhPenaltyAmt.longValue()) {
				logger.info("McrBillBuyBackRecordServiceImpl.ylBuyBack 与资方回购罚息不一致,资方罚息:"+DecimalUtils.convertFenToYuan(yhPenaltyAmt.longValue())+",我司罚息:"+ DecimalUtils.convertFenToYuan(penaltyAmt.longValue()) );
				dto.setRepSts("03");
				dto.setBuyBackFailReason("与资方回购罚息不一致,资方罚息:"+DecimalUtils.convertFenToYuan(yhPenaltyAmt.longValue())+",我司罚息:"+ DecimalUtils.convertFenToYuan(penaltyAmt.longValue()));//失败原因
				this.publicMcrBillMApiService.modifyMcrBillM(dto);
				
				this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
				return result;
			}
			if(intrAmt.longValue() != yhIntrAmt.longValue()) {
    			BaseLogUtils.info(logger, "sendYLBuyBack", "与资方回购利息不一致,资方利息:"+DecimalUtils.convertFenToYuan(yhIntrAmt.longValue())+",我司利息:"+ DecimalUtils.convertFenToYuan(intrAmt.longValue()), CurrentThreadContext.getCurrentUserName());
    			dto.setRepSts("03");
				dto.setBuyBackFailReason("与资方回购利息不一致,资方利息:"+DecimalUtils.convertFenToYuan(yhIntrAmt.longValue())+",我司利息:"+ DecimalUtils.convertFenToYuan(intrAmt.longValue()));
    			this.publicMcrBillMApiService.modifyMcrBillM(dto);
    			
    			this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
    			return result;
    		}
    		if(capAmt.longValue() != yhCapAmt.longValue()) {
    			BaseLogUtils.info(logger, "sendYLBuyBack", "与资方回购本金不一致,资方本金:"+DecimalUtils.convertFenToYuan(yhCapAmt.longValue())+",我司本金:"+ DecimalUtils.convertFenToYuan(capAmt.longValue()), CurrentThreadContext.getCurrentUserName());
    			dto.setRepSts("03");
				dto.setBuyBackFailReason("与资方回购本金不一致,资方本金:"+DecimalUtils.convertFenToYuan(yhCapAmt.longValue())+",我司本金:"+ DecimalUtils.convertFenToYuan(capAmt.longValue()));
    			this.publicMcrBillMApiService.modifyMcrBillM(dto);
    			
    			this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
    			return result;
    		}
    		
    		dto.setRepSts("01");
    		this.publicMcrBillMApiService.modifyMcrBillM(dto);
    		//更新回购状态,记录回购明细
    		this.updateMcrInformTransfer(dto, capAmt, intrAmt, penaltyAmt, totalAmt, yhCapAmt, yhIntrAmt, yhPenaltyAmt, yhTotalAmt);
			reSetMcrBillMDTO(dto);
			
			//更新还款计划
			McrBillDQC billdQc = new McrBillDQC();
			billdQc.setBillId(dto.getId());
			List<McrBillDDTO> searchList = publicMcrBillDApiService.searchMcrBillDList(billdQc);
			if(CollectionUtils.isNotEmpty(searchList)) {
				for(McrBillDDTO billD:searchList) {
					Long dCapAmt = billD.getCapAmt();//应还本金
					Long dIntrAmt = billD.getIntrAmt();//应还利息
					Long dPpaidCapAmt = billD.getPaidCapAmt();//已还本金
					Long dPaidIntrAmt = billD.getPaidIntrAmt();//已还利息
					Long dRrantAmt = billD.getRantAmt();//应还担保费
					if((dCapAmt + dIntrAmt) >  (dPpaidCapAmt+dPaidIntrAmt)) {
						billD.setIntrAmt(dIntrAmt + dRrantAmt);
						publicMcrBillDApiService.modifyMcrBillD(billD);
					}
				}
			}
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误!");
            BaseLogUtils.error(logger, "sendYLBuyBack", "推送亿联回购数据失败:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
    	
    	return result;
    }
}

